/*
 * Copyright (C) 2010-2011 Comcast Cable.  All Rights Reserved.
 */
package com.hercules.android.receiver.handlers.commands.call;

import com.hercules.android.receiver.AndroidReceiver;
import com.hercules.android.receiver.handlers.commands.XREAggregateCommandHandler;
import com.hercules.android.receiver.handlers.commands.XREObjectCommandHandler;
import com.hercules.android.receiver.model.commands.XRECallCommand;
import com.hercules.android.receiver.model.commands.XRECommand;
import com.hercules.android.receiver.model.objects.XRECommandSequence;

import java.util.List;

/**
 * This class represents the call command handler for a XRECommandSequence object. It extends from
 * XREObjectCommandHandler.
 * <p>
 * <b>Sample Usage:</b>
 *
 * <pre>
 * // create XRECommandSequenceCallCommandHandler instance
 * XRECommandSequenceCallCommandHandler handler = new XRECommandSequenceCallCommandHandler();
 *
 * // prepare receiver
 * AndroidReceiver receiver = prepareAggregateReceiver(new XRECommandSequence());
 *
 * // process execute command
 * XRECallCommand command = prepareCommand(&quot;execute&quot;);
 * handler.processCommandMessage(command, receiver);
 * </pre>
 * <p>
 * <b>Thread Safety:</b> Not thread safe since it is mutable.
 *
 * @author DanLazar, orial
 * @version 1.0
 */
public class XRECommandSequenceCallCommandHandler extends XREObjectCommandHandler {
    /**
     * Default empty constructor.
     */
    public XRECommandSequenceCallCommandHandler() {
        // empty
    }

    /**
     * Process the given command.
     *
     * @param command the command
     * @param receiver the receiver
     * @throws IllegalArgumentException if any parameter is not correct
     */
    @Override
    public void processCommandMessage(XRECommand command, AndroidReceiver receiver) {
        String methodName = "processCommandMessage";
        Helper.logEnter(getLogger(), this, methodName, command, receiver);

        try {
            // check arguments and get target object
            XRECallCommand callCommand = Helper.checkCommand(command);
            XRECommandSequence sequence = Helper.checkReceiver(receiver, callCommand, XRECommandSequence.class);

            // check list
            List<XRECommand> commands = sequence.getCommands();
            Helper.checkNotNull(commands, "targetXRECommandSequence.commands");

            // get aggregate command handler
            XREAggregateCommandHandler handler = receiver.getCommandHandler();
            Helper.checkNotNull(handler, "receiver.commandHandler");

            // execute each commands
            for (XRECommand xreCommand : commands) {
                handler.onCommandMessage(xreCommand, receiver);
            }

            Helper.logExit(getLogger(), this, methodName);
        } catch (IllegalArgumentException e) {
            Helper.logError(getLogger(), this, methodName, e);
        } catch (IllegalStateException e) {
            Helper.logError(getLogger(), this, methodName, e);
        }
    }
}